// Copyright 2011 Dave Ruest.  All rights reserved.

/**
 * @fileoverview This file contains game control helpers. The pause button, the score button,
 * and the start and end dialogs.
 * 
 * @author Dave Ruest dave.ruest@gmail.com
 */

/**
 * Pause constructor. A helper object to draw the play/pause button.
 * @param {Game} game
 * @returns {Pause}
 */
function Pause(game) {
	this.game = game;
	this.x = game.status.size / 10;
	this.y = 0;
	this.paused = true;
};

Pause.prototype.draw = function() {
	var c = 1;
	if (this.paused) {
		c = 2;
	}
	this.game.status.draw(this.x, this.y, 0, c);
};

Pause.prototype.onClick = function(x, y) {
	if (game.start) {
		game.start.onClick();
		return;
	}
	
	var size = this.game.status.size;
	if (x > this.x && x < this.x + size && y < size) {
		this.paused = !this.paused;
	}
};

/**
 * Score constructor. A helper object to store and draw the player's score. 
 * @param {Game} game
 * @returns {Score} 
 */
function Score(game) {
	this.game = game;
	this.value = 3;
	
	this.ix = game.status.size / 10;
	this.iy = game.height - game.status.size;
	
	this.tx = this.ix + game.status.size / 2;
	this.ty = this.iy + game.status.size / 2;
}

Score.prototype.draw = function() {
	this.game.status.draw(this.ix, this.iy, 0, 0);
	
	this.game.graphics.textAlign = "center";
	this.game.graphics.textBaseline = "middle";
	this.game.graphics.font = "bold 40px Arial";
	
	this.game.graphics.fillStyle = "#ffeeaa";
	this.game.graphics.fillText(this.value, this.tx, this.ty);
	
	this.game.graphics.fillStyle="#2b2200";
	this.game.graphics.strokeText(this.value, this.tx, this.ty);
};

/**
 * Start constructor. Displays a start dialog while the game is in the loaded but 
 * not yet started "state". Sets game.start to null when clicked, effectively starting
 * the game loop.
 * @param game
 */
function Start(game) {
	this.game = game;
}

Start.prototype.draw= function() {
	var x = game.width / 2 - game.startDialog.width / 2;
	var y = game.height / 2 - game.startDialog.height / 2;
	this.game.graphics.drawImage(game.startDialog, x, y);
};

Start.prototype.onClick = function() {
	this.game.start = null;
	this.game.pause.paused = false;
};

/**
 * End constructor. Displays an end dialog when the game is finished.
 * Creates a new game when clicked.
 * @param game
 */
function End(game) {
	this.game = game;
	// image draw coordinates
	this.x = game.width / 2 - game.endDialog.size / 2;
	this.y = game.height / 2 - game.endDialog.size / 2;
	// score draw coordinates
	this.tx = this.x + 58; // ugh... oh well
	this.ty = this.y + 140;
}

End.prototype.draw = function() {
	var score = this.game.score.value;
	var r = 0;
	if (score <= 0) {
		r = 1;
	}
	this.game.endDialog.draw(this.x, this.y, 0, r);
	
	this.game.graphics.textAlign = "center";
	this.game.graphics.textBaseline = "middle";
	this.game.graphics.font = "bold 60px Arial";
	
	this.game.graphics.fillStyle = "#ffeeaa";
	this.game.graphics.fillText(score, this.tx, this.ty);
	
	this.game.graphics.fillStyle="#2b2200";
	this.game.graphics.strokeText(score, this.tx, this.ty);
};

End.prototype.onClick = function() {
	game = new Game();
	game.pause.paused = false;
};
